﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>MyCompany.MyProject</Solution>
// <Project>MyCompany.MyProject.Application</Project>
// <FileName>SearchRecordAppService.cs</FileName>
// <CreateTime>2017-04-22 21:47</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-22 21:47" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Vecen.BangChuang.BangChuang.Common.Dtos;

namespace Vecen.BangChuang.BangChuang.Common {
    /// <summary>
    /// 搜索记录
    /// </summary>
    public class SearchRecordAppService: BangChuangAppServiceBase, ISearchRecordAppService {
        private readonly IRepository<SearchRecord> _searchRecordRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchRecordRepository"></param>
        public SearchRecordAppService(IRepository<SearchRecord> searchRecordRepository) {
            _searchRecordRepository = searchRecordRepository;
        }

        /// <summary>
        /// 获取搜索记录
        /// </summary>
        /// <returns></returns>
        public Task<ListResultDto<SearchRecordOutDto>> GetSearchRecordAsync() {
            return Task.Run(() => {
                var entities =
                    _searchRecordRepository.GetAll()
                        .Where(c => c.CreatorUserId == AbpSession.UserId)
                        .OrderByDescending(c => c.LastSearchTime)
                        .ToList();

                return new ListResultDto<SearchRecordOutDto>(entities.MapTo<List<SearchRecordOutDto>>());
            });

        }
    }
}